package od.igor.petrov.bcontrol;

import java.util.ArrayList;
import java.util.Collections;

import od.igor.petrov.derby.DerbyDAO;
import od.igor.petrov.utils.TestTableBean;
import od.igor.petrov.utils.TestTableBeanFirmComparator;
import od.igor.petrov.utils.TestTableBeanIdComparator;
import od.igor.petrov.utils.TestTableBeanItemComparator;

public class DataPreparation {
	
	public DataPreparation(){
		daoClazz = new DerbyDAO();
	}
	
	protected void retrieveUnsortedCollection(){
		collectionOfBeans = daoClazz.selectValues();
		if(collectionOfBeans == null || collectionOfBeans.size() == 0){
			collectionOfBeans = new ArrayList<TestTableBean>(1);
			collectionOfBeans.add(new TestTableBean(0, "DEFAULT0", "DEFAULT0"));
		}
	}
	
	//Simple method to populate DB with initial data
	public void populateDB(){
		daoClazz.insertValues();
	}
	
	//Method to get the SORTED collection by    I D
	public ArrayList<TestTableBean> getSortedCollectionByID(boolean orderDesc){
		retrieveUnsortedCollection();
		Collections.sort(collectionOfBeans, new TestTableBeanIdComparator(orderDesc));
		return collectionOfBeans;
	}
	
	//Method to get the SORTED collection by    I T E M
	public ArrayList<TestTableBean> getSortedCollectionByItem(boolean orderDesc){
		retrieveUnsortedCollection();
		Collections.sort(collectionOfBeans, new TestTableBeanItemComparator(orderDesc));
		return collectionOfBeans;
	}
	
	//Method to get the SORTED collection by    F I R M
	public ArrayList<TestTableBean> getSortedCollectionByFirm(boolean orderDesc){
		retrieveUnsortedCollection();
		Collections.sort(collectionOfBeans, new TestTableBeanFirmComparator(orderDesc));
		return collectionOfBeans;
	}
	
	private ArrayList<TestTableBean> collectionOfBeans = null;
	private DerbyDAO daoClazz = null;

}
